/**
 * @description 天地图任务
*/

const Task = require('./task.js')
const config = global.config
const tile = config.tile || {}

class Tianditu extends Task {
  /**
  * @function 获取下载地址
  * @param {Object} item 瓦片对象
  */
  getUrl (item) {
    let x = item.x, y = item.y, z = item.z
    let params = this.layer.mapProvider.params
    let url = this.layer.layerProvider.url
    let s = params.subdomains[Math.floor(Math.random() * params.subdomains.length)] // 服务主机
    let tk = params.tk[Math.floor(Math.random() * params.tk.length)] // token
    url = url.replace('{s}', s).replace('{tk}', tk)
    let query = {
      service: this.layer.layerProvider.type,
      request: 'GetTile',
      version: '1.0.0',
      layer: this.layer.layer,
      tileMatrixSet: 'w',
      tilematrix: z,
      tilerow: y,
      tilecol: x,
      style: 'default',
      format: 'tiles'
    }
    url = this.mergeQuery(url, query)
    return url
  }
}

module.exports = Tianditu